package com.ajie.controller;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.session.SaSessionCustomUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.ajie.config.annotation.RestControllerMapping;
import com.ajie.entity.BaseEntity;
import com.ajie.entity.dto.Option;
import com.ajie.entity.vo.RoleAuthority;
import com.ajie.model.PageParams;
import com.ajie.model.Result;
import com.ajie.entity.SysRole;
import com.ajie.service.SysRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;

import java.util.List;
import java.util.Map;

/**
 * 角色表;(sys_role)表控制层
 * @author: ajie
 * @date : 2024-7-7
 */
@Tag(name = "角色表")
@RestControllerMapping("/sysRole")
@ApiSupport(author = "ajie:20999@163.com", order = 9)
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    @PostMapping("page")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "分页查询")
    public Result<Page<SysRole>> pageQuery(@RequestBody PageParams<SysRole> pageParams) {
        // 1.查询参数
        SysRole params = pageParams.getParams();
        LambdaQueryWrapper<SysRole> lqw = new LambdaQueryWrapper<>();
        // 2.查询条件
        lqw.like(params.getName() != null, SysRole::getName, params.getName());
        lqw.like(params.getCode() != null, SysRole::getCode, params.getCode());
        // 3.分页查询
        return Result.success(sysRoleService.page(pageParams.getPage(), lqw));
    }

    @GetMapping("roleOptions")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "角色下拉列表")
    public Result<List<Option>> roleOptions() {
        // 1.查询参数
        QueryWrapper<SysRole> lqw = new QueryWrapper<>();
        lqw.select("name AS label", "id AS value");
        List<Map<String, Object>> maps = sysRoleService.listMaps(lqw);
        List<Option> data = BeanUtil.copyToList(maps, Option.class);
        // 3.分页查询
        return Result.success(data);
    }

    @PostMapping("insert")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "新增数据")
    public Result<Boolean> insert(@RequestBody SysRole sysRole) {
        if (sysRoleService.save(sysRole)) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("update")
    @ApiOperationSupport(order = 3)
    @Operation(summary = "更新数据")
    public Result<Boolean> update(@RequestBody SysRole sysRole) {
        if (sysRoleService.updateById(sysRole)) {
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("/delete/{id}")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "通过主键删除数据", parameters = {
            @Parameter(name = "id", description = "主键", required = true, in = ParameterIn.PATH)
    })
    public Result<Boolean> deleteById(@PathVariable String id){
        if (sysRoleService.removeById(id)) {
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("getPermissions/{roleId}")
    @ApiOperationSupport(order = 5)
    @Operation(summary = "获取角色拥有的权限集合", parameters = {
            @Parameter(name = "roleId", description = "角色id", required = true, in = ParameterIn.PATH)
    })
    public Result<List<String>> getPermissions(@PathVariable String roleId) {
        List<String> list = sysRoleService.getPermissions(roleId);
        return Result.success(list);
    }

    @PostMapping("check")
    @ApiOperationSupport(order = 6)
    @Operation(summary = "获取角色拥有的权限集合")
    public Result<Boolean> check(@RequestBody RoleAuthority authority) {
        if (sysRoleService.check(authority)) {
            // 得到角色信息
            SysRole role = sysRoleService.getById(authority.getRoleId());
            // 删除该角色的缓存
            SaSessionCustomUtil.deleteSessionById("role-" + role.getCode());
            return Result.success(Boolean.TRUE);
        }
        return Result.error();
    }


}